

import static org.junit.Assert.*;

import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import codeformatter.Formatter;
import exceptions.ReadStreamException;
import exceptions.WriteStreamException;
import exceptions.WrongBracketAmountException;

public class TestFormatter extends TestCase {
	Formatter formatter = new Formatter();
	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testFormatWithEmptyInput() {
		ByteArrayInputStream bais = new ByteArrayInputStream("".getBytes());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String rightStr = "";
		try {
			formatter.format(bais, baos);
		} catch (ReadStreamException e) {
			fail("Failed to read from the stream");
		} catch (WriteStreamException e) {
			fail("Failed to write to the stream");
		} catch (WrongBracketAmountException e) {
			fail("Wrong amount of brackets detected");
		}
		assertEquals(baos.toString(),rightStr);
	}
	@Test
	public void testFormatWithNoLineFeedInput() {
		ByteArrayInputStream bais = new ByteArrayInputStream(("ByteArrayOutputStream baos = new ByteArrayOutputStream();" +
				"try { formatter.format(bais, baos);} catch (ReadStreamException e) {fail(\"Failed to read from the stream\");}").getBytes());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String rightStr = "ByteArrayOutputStream baos = new ByteArrayOutputStream();\r\n" +
				"try {\r\n    formatter.format(bais, baos);\r\n}\r\ncatch (ReadStreamException e) {\r\n    fail(\"Failed to read from the stream\");\r\n}\r\n";		
		try {
			formatter.format(bais, baos);
		} 
		catch (ReadStreamException e) {
			fail("Failed to read from the stream");
		} catch (WriteStreamException e) {
			fail("Failed to write to the stream");
		} catch (WrongBracketAmountException e) {
			fail("Wrong amount of brackets detected");
		}
		String str = baos.toString();
		for(int i = 0; i < str.length(); i ++){
			if(str.charAt(i) != rightStr.charAt(i)){
				char c1 = str.charAt(i);
				char c2 = rightStr.charAt(i);
				boolean b = false;
			}
		}
		boolean b = str.equals(rightStr);
		int l1 = str.length();
		int l2 = rightStr.length();
		assertEquals(rightStr, str);
	}
	@Test
	public void testFormatWithCommentary() {
		ByteArrayInputStream bais = new ByteArrayInputStream(("ByteArrayOutputStream baos = new ByteArrayOutputStream();//" +
				"try {form/*atter.format(bais, baos);} catch (ReadStreamException e) {fail(\"Faile*/d to read from the stream\");").getBytes());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String rightStr = "ByteArrayOutputStream baos = new ByteArrayOutputStream();\r\n//" +
				"try {form/*atter.format(bais, baos);} catch (ReadStreamException e) {fail(\"Faile*/d to read from the stream\");";		
		try {
			formatter.format(bais, baos);
		} 
		catch (ReadStreamException e) {
			fail("Failed to read from the stream");
		} catch (WriteStreamException e) {
			fail("Failed to write to the stream");
		} catch (WrongBracketAmountException e) {
			fail("Wrong amount of brackets detected");
		}
		assertEquals(baos.toString(), rightStr);
	}	
}
